package za.co.smartmi.web.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import static org.springframework.web.bind.annotation.RequestMethod.GET;

import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import za.co.smartmi.form.ClientForm;
import za.co.smartmi.form.WebUserSearchForm;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import za.co.smartmi.form.ClientSearchForm;
import za.co.smartmi.model.Client;
import za.co.smartmi.service.ClientService;

@Controller
@RequestMapping("/domain/client/")
public class ClientController {
    private static final Logger log = Logger.getLogger(ClientController.class);
    @Autowired
    private ClientService clientService;
    
    private final String searchPage = "/domain/client/search";
//    private final String showUsersPage = "domain/customerportal/show_users";
    
    @RequestMapping(value = "search", method = RequestMethod.POST)
    public String list(@Valid ClientSearchForm searchForm, BindingResult result, Model model) throws Exception {
        log.debug("IN POST");
        List<Client> clients = new ArrayList<Client>();
        if (result.hasErrors()) {
            log.debug("ERRORS " + result.getFieldErrors());
            
        } else {
            clients = clientService.getClientsBySearch(searchForm);
        }
        log.debug("clients " + clients.size());
        ClientForm clientForm = new ClientForm();
        clientForm.setClientList(clients);
        model.addAttribute("clientForm", clientForm);
        model.addAttribute("clientCount", clients.size());
        return searchPage;
    }

    @RequestMapping(value = "search", method = GET)
    public String search(Model model) {
        model.addAttribute("clientSearchForm", new ClientSearchForm());
        model.addAttribute("clientCount", 0);
        log.debug("IN GET");
        return searchPage;
    }
    
//   @RequestMapping(value = "save", method = RequestMethod.POST)
//    public ModelAndView save(@ModelAttribute("webUserForm") WebUserForm userForm, @ModelAttribute("webUserSearchForm") WebUserSearchForm searchForm) {
//        log.debug("In save userForm.getWebUserList() "+ userForm.getWebUserList());
//        
//        String targetPage = showUsersPage;
//        List messages = new ArrayList();
//        List< WebUser> webUsers = userForm.getWebUserList();
//            Predicate predicate = new Predicate() {
//
//                    @Override
//                    public boolean evaluate(Object object) {
//                        WebUser wu = (WebUser) object;
//                        if (wu.getProfileUnique()!=null) {
//                            return true;
//                        } else {
//                            return false;
//                        }
//                        
//                    }
//            };
//        //we filter out all those users that were not selected. Filter will actually remove them from the collection.
//        CollectionUtils.filter(webUsers, predicate);
//        if (webUsers !=null && webUsers.size() > 0) {
//            try {
//                customerPortalService.writeWebUserToCSVFile(webUsers);
//                messages.add("portal.activation.msg.success");
//            } catch (InterruptedException ex) {
//                log.error(ex.getLocalizedMessage() , ex);
//                messages.add("exception.caught.user.file");
//                
//            } catch (IOException ex) {
//                log.error(ex.getLocalizedMessage() , ex);
//                messages.add("exception.caught.user.file");          
//            }
//        } else { 
//            messages.add("error.no.users.selected");          
//        }
//        Map pageObjects = new HashMap();
//        pageObjects.put("webUserForm", userForm);
//        pageObjects.put("messages", messages);
//        pageObjects.put("webUserSearchForm", searchForm);
//        return new ModelAndView(targetPage, pageObjects);
//    }    
//    
}
